%TCS acquisition session
clear all
filename=['AttentionSSEP_S',input('Participant number and _date?','s')];
%%
COMport='COM3';
display_params=0;           %binary'''
neutral_temp=32;            %�C
surface_area='11111';       %11111=all
%unused parameters
target_temperature=45;      %�C
temperature_slope=20;      %�C/s
return_slope=300;
trigger_code='255010';
feedback_duration=9999;     %ms

%generate white noise 
%Fs = 96000;  % sampling frequency in Hz
%te = 80; % signal duration in seconds
%y = randn(te*Fs,1); % generate White noise of duration te
%% initialization 
disp('Initializing')
NI=NI_initialize;
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % Params for the NI (jack to launch stim). 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 launch_stim_NI = 1 ;            % launch the stim with the NI, and also 
%                                 % send a trigger on AO1. 
%                                 % Otherwise: stim launched through USB,
%                                 % need to use red cable to send triggers.
 SR = 1000 ;                     %
 NI_time = 1 ; %time_one_epoch ; % time to send outputs and read inputs with NI. 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
TCS=USB_TCSII_initialize(COMport);
calibrateVAS=VAS_calibration(NI,10); %calibrateVAS=VAS_calibration(NI,10) -- VAS_calibration(NI,recordingDuration)
MinVAS=min(calibrateVAS);
MaxVAS=max(calibrateVAS);
    %%
    t1=350;%target temp 1 in tenth of �C
    t2=500;%target temp 2
    np=15; %n periods
    

    p=5; %period duration
 
    pp=round((.5+sin(-pi/2-(2*pi)/19*(0:19))/2)*(t2-t1)+t1); %generate 1 sine
    tb=100*p/length(pp); % time bin length in 10ms
    
    % generating the serie of numbers corresponding to 1 period (each group
    % of 6 correspond to 1 time point
    period={};
    period(1:2:2*length(pp))={sprintf('%03d',tb)};
    for i=2:2:2*length(pp)
    period(i)={sprintf('%d',pp(i/2))};
    end
    %making the period array a period string
    P=[];
    for i=1:length(period)
        P=[P char(period(i))];
    end
    
    NumPairs=length(pp)*np; % computing the number of defined time points 
    
    
    h=['Uw11111' sprintf('%03d',NumPairs)]; %setting the head of the command
    
    AllP=repmat(P,1,np); %creating a string wigh all the periods
    
    CP=[h AllP];   %assembling the head and periods strings
      
   %% Demonstration NC stim + t� recording
disp('Demo stimulations')
disp('setting')
feedback_duration=np*p*1000;
stimulus_duration=np*p*1000;
print_to_serial(TCS,'Y9999',0); %set temperature recording duration to 9999 ms
print_to_serial(TCS,'N320',0); %set neutral temp to 32�C
print_to_serial(TCS,'Ob',0); %enabling temeprature feedback reading
print_to_serial(TCS,'Ue11111',0);%enabling custom profile commands on all zones
print_to_serial(TCS,CP,0); % sending custom profile to the TCS
disp('Press ENTER key to launch')
pause()
[temp]=TCSII_launch_USB(TCS,feedback_duration,stimulus_duration);
beep()
plot(temp)


%% thermonociceptive block 1
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

rat=' - recording trials';
rating_thermo_b1=zeros(10,1); %creating matrix for VAS input (rating)
pain_thermo_b1=zeros(10,1);
first_condition=input('Starting condition for thermonociceptive stimulation? (Distraction = 1 / Baseline = 0)'); 

    if first_condition == 0
        second_condition = 'Distraction';
   
    else 
        second_condition = 'Baseline';
    end 

nStim=10;
    print_to_serial(TCS,'Y0000',0);
    print_to_serial(TCS,'Ue11111',0);
    print_to_serial(TCS,'Os',0);
    print_to_serial(TCS,'N320',0);
    print_to_serial(TCS,CP,0)
    
    recordingDuration=np*p;
    
    disp(['Thermo ',first_condition ,rat])
        for i=1:nStim
            
            disp(['stimuli ',num2str(i)])
            disp('ready, press any key to stimulate')
            pause()  
            disp('stimulating')
            %sound(y,Fs,24); %play white noise 
            %pause(5) %to start audio a bit before trigger EEG
            [data,temp]=TCSII_launch_NI(TCS,NI,recordingDuration,1,0,0);  %[data,time,temp]=TCSII_launch_NI(TCS,NI,recording_duration,foreperiod,postperiod,feedback_duration)
        print_to_serial(TCS,'N320',0);
     
        beep(); 
        
        pause(10); %10s pause to give subject time to slide VAS ruler after stim
        beep();
        
        single_scan = NI.inputSingleScan; % takes one scan of input channel (VAS)
        rating_thermo_b1(i,1)=single_scan; %fill data into table
        
        painfulness= input('Did you perceive this stimulation as painful? (Yes = 1, No = 0)')
        pain_thermo_b1(i,1)=painfulness; %fill pain data into table
        
        end
 save(filename)

 %% vibrotactile block 1
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rat=' - recording trials';
rating_vibro_b1=zeros(10,1); %creating matrix for VAS input (rating)
pain_vibro_b1=zeros(10,1); %for painfulness question answers

vibr_first_condition=input('Starting condition for vibrotactile stimulation? (Distraction = 1 / Baseline = 0)'); 

    if vibr_first_condition == 0
        vibr_second_condition = 'Distraction';
   
    else 
        vibr_second_condition = 'Baseline';
    end 

nStim=10;
    disp(['Block ',vibr_first_condition,rat])
        for i=1:nStim
           
           disp('Press any key to register VAS') 
           pause() 
           single_scan = NI.inputSingleScan; % takes one scan of input channel (VAS)
            disp(['VAS ',num2str(i),'registered'])
            
           painfulness= input('Did you perceive this stimulation as painful? (Yes = 1, No = 0)')
           pause()
        beep();
         
        rating_vibro_b1(i,1)=single_scan; %fill data into table
        pain_vibro_b1(i,1)=painfulness; %fill pain data into table
        
        end
        
save(filename)
        
%% thermonociceptive block 2
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
rat=' - recording trials';
rating_thermo_b2=zeros(10,1); %creating matrix for VAS input (rating) for second block of thermo stimuli
pain_thermo_b2=zeros(10,1);

nStim=10;
    print_to_serial(TCS,'Y0000',0);
    print_to_serial(TCS,'Ue11111',0);
    print_to_serial(TCS,'Os',0);
    print_to_serial(TCS,'N320',0);
    print_to_serial(TCS,CP,0)
    recordingDuration=np*p;
    
    disp(['Thermo ',second_condition ,rat])
        for i=1:nStim
            
            disp(['stimuli ',num2str(i)])
            disp('ready, press any key to stimulate and play white noise')
            pause()  
            disp('stimulating')
            %sound(y,Fs,24); %play white noise 
            %pause(5)
            [data,temp]=TCSII_launch_NI(TCS,NI,recordingDuration,1,0,0);  %[data,time,temp]=TCSII_launch_NI(TCS,NI,recording_duration,foreperiod,postperiod,feedback_duration)
        print_to_serial(TCS,'N320',0);
     
        beep(); 
        
        pause(10); %10s pause to give subject time to slide VAS ruler after stim
        beep();
        
        single_scan = NI.inputSingleScan; % takes one scan of input channel (VAS)
        rating_thermo_b2(i,1)=single_scan; %fill data into table
        
        painfulness= input('Did you perceive this stimulation as painful? (Yes = 1, No = 0)')
        pain_thermo_b2(i,1)=painfulness; %fill pain data into table
        end

 save(filename) 
 
%% vibrotactile block 2
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rating_vibro_b2=zeros(10,1); %creating matrix for VAS input (rating)
pain_vibro_b2=zeros(10,1);

nStim=10;
    disp(['Block ',vibr_second_condition,rat])
        for i=1:nStim
           disp(['stimuli ',num2str(i)])
           disp('Press any key to register VAS') 
           pause() 
           single_scan = NI.inputSingleScan; % takes one scan of input channel (VAS)
            disp(['VAS ',num2str(i),'registered'])
            
           painfulness= input('Did you perceive this stimulation as painful? (Yes = 1, No = 0)')
        beep();
      
        rating_vibro_b2(i,1)=single_scan; %fill data into table
        pain_vibro_b2(i,1)=painfulness; %fill pain data into table
        end
        
save(filename)      

